#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_MainWindow.h"
#include "UiCommon.h"
#include "SingleMassDialog.h"
#include "CalibDialog.h"
#include "AcquireDialog.h"
#include "GatherThread.h"
#include "Config.h"
#include <array>
#include <QTimer>
#include "PlotWidget.h"
#include "GatherData9982.h"
#include "DataGatherUSB9982.h"
#include "DataGatherUSB9982Sim.h"

#ifdef _DEBUG
#define SIM_9982 0
#else
#define SIM_9982 0
#endif

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = Q_NULLPTR);
    ~MainWindow();

protected slots:
    void on_actionNew_triggered();
    void on_actionOpen_triggered();    
    void on_actionExit_triggered();
        
    void on_actionAcqStart_triggered();
    void on_actionAcqStop_triggered();
    void on_actionAcqSingle_triggered();
    void on_actionAcqReset_triggered();

    void on_actionAcquire_triggered();
    void on_actionAcquireSave_triggered();

    void on_actionSumWindow_triggered();
    void on_actionEachWindow_triggered();
    void on_actionSingleWindow_triggered();

    void on_actionCabli_triggered();
    void on_actionHelp_triggered();

    void on_btnCancelSetting_clicked();
    void on_btnInitSetting_clicked();
    void on_btnSaveSetting_clicked();

    void onBtnDOClicked();
    void onBtnHRF9828Set();
    void onBtnHRF9828Back();
    void onHRF9828Data(QByteArray data);
    void onHRF9828Error(QString error);

    void onAcquireData(QDateTime time,GatherDataBuffer *data);
    void onAcquireError(QString error);
    void onAcquireFinish();
    void onFrameSelect(int index);

    void onTimerOut();    

private:
    virtual void resizeEvent(QResizeEvent *event) override;
    void beginSave();
    void beginGather(bool signle);

    QGroupBox *createGroupSpin(QString groupname, QStringList items, QList<QLabel*>  &spinLabel,QList<QDoubleSpinBox*> &spinBoxList);
    QGroupBox *createGroupBtn(QString groupname, QStringList items, QList<QPushButton*> &btnList);
    void updateButtonIcon(QPushButton *btn);
    void updateAcqStatus();    

    void initControls();
    void initProject();    
    void createSubWindow();
    void layoutSubWindow();

    void uiToParamHRF9828();
    void ParamHRF9828ToUi();
    void updateHRF9828Name();
    void syncGatherParam();
    void parseHRF9828Data(QByteArray &buffer);
    
    GatherThread m_gather;
#if SIM_9982
    DataGatherUSB9982Sim m_acqGather;
#else
    DataGatherUSB9982 m_acqGather;    
#endif

    Config m_config;
        
    QTimer m_timer;
    int m_timeIndex;

    QList<QLabel*> m_AOLabelList; 
    QList<QDoubleSpinBox*> m_AOList;
    QList<QLabel*> m_AILabelList;
    QList<QDoubleSpinBox*> m_AIList;
    QList<QPushButton*> m_DOList;
    QList<QPushButton*> m_DIList;
    
    QList<QLineEdit*> m_SetLineList;
    QList<QDoubleSpinBox*> m_SetSpinList;    

    QList<QPushButton*> m_setBtnList;
    QList<QPushButton*> m_backBtnList;
    QVector<int> m_setRange;
        
    QDateTime m_dataTime;
    QVector<float> m_dataBuff;
    QDateTime m_updateTime;
    bool m_needUpdate;
    
    PlotWidget *m_sum;
    PlotWidget *m_tic;
    QVector<PlotWidget*> m_singleWindow;
    GatherFile m_file;    

    Ui::MainWindowClass ui;
};
